package com.apes.hr.base.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.SkillLevel;
import com.apes.hr.base.repository.SkillLevelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：技能证书等级
 *
 * @author HuJianLin
 * @create 2018-04-28 14:35
 */
@Service("hrSkillLevelService")
public class SkillLevelService extends DomainService {
    @Autowired
    public SkillLevelRepository skillLevelRepository;

    public SkillLevel save(SimpleRequest request) {
        SkillLevel skillLevel = request.getO(SkillLevel.class);
        return create(skillLevel);
    }

    public SkillLevel create(SkillLevel skillLevel) {
        return skillLevelRepository.saveAndFlush(skillLevel);
    }


    public Page<SkillLevel> findByAll (SimpleRequest request) {
        List<Map> filters = request.get("condition");
        return skillLevelRepository.findAll(JpaDsl.toCriteria(filters),PageRequest.of(request.get("page"), request.get("size")));
    }

    public String delete(SimpleRequest request) throws Exception  {
        Integer versions = request.get("version");
        SkillLevel skillLevel = request.getO(SkillLevel.class);
        if (skillLevel.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        skillLevelRepository.delete(skillLevel);
        return "删除成功";
    }

    public SkillLevel findByOne(Integer id) {
        return skillLevelRepository.findByOne(id);
    }
}
